| Conditions | 1 |
| Paths | 216 |
| Total Lines | 141 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 27 | var et2_radiobox = (function(){ "use strict"; return et2_inputWidget.extend( |
||
| 28 | { |
||
| 29 | attributes: { |
||
| 30 | "set_value": { |
||
| 31 | "name": "Set value", |
||
| 32 | "type": "string", |
||
| 33 | "default": "true", |
||
| 34 | "description": "Value when selected" |
||
| 35 | }, |
||
| 36 | "ro_true": { |
||
| 37 | "name": "Read only selected", |
||
| 38 | "type": "string", |
||
| 39 | "default": "x", |
||
| 40 | "description": "What should be displayed when readonly and selected" |
||
| 41 | }, |
||
| 42 | "ro_false": { |
||
| 43 | "name": "Read only unselected", |
||
| 44 | "type": "string", |
||
| 45 | "default": "", |
||
| 46 | "description": "What should be displayed when readonly and not selected" |
||
| 47 | } |
||
| 48 | }, |
||
| 49 | |||
| 50 | legacyOptions: ["set_value", "ro_true", "ro_false"], |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor |
||
| 54 | * |
||
| 55 | * @memberOf et2_radiobox |
||
| 56 | */ |
||
| 57 | init: function() { |
||
| 58 | this._super.apply(this, arguments); |
||
| 59 | |||
| 60 | this.input = null; |
||
| 61 | this.id = ""; |
||
| 62 | |||
| 63 | this.createInputWidget(); |
||
| 64 | }, |
||
| 65 | transformAttributes: function(_attrs) { |
||
| 66 | this._super.apply(this, arguments); |
||
| 67 | var readonly = this.getArrayMgr('readonlys').getEntry(this.id); |
||
| 68 | if(readonly && readonly[_attrs.set_value]) |
||
| 69 | { |
||
| 70 | _attrs.readonly = readonly[_attrs.set_value]; |
||
| 71 | } |
||
| 72 | }, |
||
| 73 | |||
| 74 | createInputWidget: function() { |
||
| 75 | this.input = jQuery(document.createElement("input")) |
||
| 76 | .val(this.options.set_value) |
||
| 77 | .attr("type", "radio") |
||
| 78 | .attr("disabled", this.options.readonly); |
||
| 79 | |||
| 80 | this.input.addClass("et2_radiobox"); |
||
| 81 | |||
| 82 | this.setDOMNode(this.input[0]); |
||
| 83 | }, |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Overwritten to set different DOM level ids by appending set_value |
||
| 87 | * |
||
| 88 | * @param _id |
||
| 89 | */ |
||
| 90 | set_id: function(_id) |
||
| 91 | { |
||
| 92 | this._super.apply(this, arguments); |
||
| 93 | |||
| 94 | this.dom_id = this.dom_id.replace('[]', '')+'-'+this.options.set_value; |
||
| 95 | if (this.input) this.input.attr('id', this.dom_id); |
||
| 96 | }, |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Default for radio buttons is label after button |
||
| 100 | * |
||
| 101 | * @param _label String New label for radio button. Use %s to locate the radio button somewhere else in the label |
||
| 102 | */ |
||
| 103 | set_label: function(_label) { |
||
| 104 | if(_label.length > 0 && _label.indexOf('%s')==-1) |
||
| 105 | { |
||
| 106 | _label = '%s'+_label; |
||
| 107 | } |
||
| 108 | this._super.apply(this, [_label]); |
||
| 109 | }, |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Override default to match against set/unset value AND iterate over all siblings with same id |
||
| 113 | * |
||
| 114 | * @param {string} _value |
||
| 115 | */ |
||
| 116 | set_value: function(_value) |
||
| 117 | { |
||
| 118 | this.getRoot().iterateOver(function(radio) |
||
| 119 | { |
||
| 120 | if (radio.id == this.id) |
||
| 121 | { |
||
| 122 | radio.input.prop('checked', _value == radio.options.set_value); |
||
| 123 | } |
||
| 124 | }, this, et2_radiobox); |
||
| 125 | }, |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Override default to iterate over all siblings with same id |
||
| 129 | * |
||
| 130 | * @return {string} |
||
| 131 | */ |
||
| 132 | getValue: function() |
||
| 133 | { |
||
| 134 | var val = this.options.value; // initial value, when form is loaded |
||
| 135 | this.getRoot().iterateOver(function(radio) |
||
| 136 | { |
||
| 137 | if (radio.id == this.id && radio.input && radio.input.prop('checked')) |
||
| 138 | { |
||
| 139 | val = radio.options.set_value; |
||
| 140 | } |
||
| 141 | }, this, et2_radiobox); |
||
| 142 | |||
| 143 | return val == this.options.set_value ? this.options.set_value : null; |
||
| 144 | }, |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Overridden from parent so if it's required, only 1 in a group needs a value |
||
| 148 | * |
||
| 149 | * @param {array} messages |
||
| 150 | * @returns {Boolean} |
||
| 151 | */ |
||
| 152 | isValid: function(messages) { |
||
| 153 | var ok = true; |
||
| 154 | |||
| 155 | // Check for required |
||
| 156 | if (this.options && this.options.needed && !this.options.readonly && !this.disabled && |
||
| 157 | (this.getValue() == null || this.getValue().valueOf() == '')) |
||
| 158 | { |
||
| 159 | if(jQuery.isEmptyObject(this.getInstanceManager().getValues(this.getInstanceManager().widgetContainer)[this.id.replace('[]', '')])) |
||
| 160 | { |
||
| 161 | messages.push(this.egw().lang('Field must not be empty !!!')); |
||
| 162 | ok = false; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | return ok; |
||
| 166 | } |
||
| 167 | });}).call(this); |
||
| 168 | et2_register_widget(et2_radiobox, ["radio"]); |
||
| 448 |